<?php
namespace app\controller;

use think\Request;
use think\facade\Db;

class Log extends Common
{

	/**
	 * 操作日志
	 *
	 * @return
	 */
	public function operRecord(Request $request)
	{
		if( $request->isDelete() ) {
			if($this->hasPermission($request->sessInfo, 'oper-record:delete')) {
				return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
			}
			$beginTime = microtime(true);
			Db::table('cfg_logs')->where('companyId', $request->sessInfo['companyId'])->where('type', 2)->where('time', '<=', time() - ( 86400 * 90 ))->delete();
			$endTime = microtime(true);
			$this->cfg_write_oper($request, $request->sessInfo['token'], $request->sessInfo['companyId'], $request->sessInfo['nickname'],
			$request->sessInfo['username'], round($endTime - $beginTime, 3), '操作日志', '清除三个月之前日志', 0, null);
			return json(['code'=> 0, 'msg'=> '删除成功!']);
		}

		if( $request->isGet() ) {

			if($this->hasPermission($request->sessInfo, 'oper-record:read')) {
				return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
			}

			$where = [];
			$limit = $request->param('limit/d', 20);
			$sort = $request->param('sort', 'id', 'trim');
			$order = $request->param('order', 'desc', 'trim');

			array_push( $where, ['companyId', '=', $request->sessInfo['companyId'] ] );
			array_push( $where, ['type', '=', 2 ] );

			$username = $request->param('username', '', 'trim');
			if($username) {
				array_push( $where, ['user', 'like',  "%{$username}%" ] );
			}

			$model = $request->param('model', '', 'trim');
			if($model) {
				array_push( $where, ['model', 'like',  "%{$model}%" ] );
			}

			$createTimeStart = $request->param('createTimeStart', '', 'trim');
			$createTimeEnd = $request->param('createTimeEnd', '', 'trim');
			if( $createTimeStart && $createTimeEnd ) {
				array_push( $where, ['time', 'between', cloud_datetime_format( $createTimeStart, $createTimeEnd, true) ] );
			}

			$query = Db::name('cfg_logs')->field(' `id`, `user`, `nickname`, `model`, `description`, `state`, `method`, `device`, `browser`, `cookie`, `referer`,
				`operType` as spendTime, `url`, FROM_UNIXTIME(time, \'%Y-%m-%d %H:%i:%s\') as `time`, `ip`, `operMethod`, `param`')
				->where($where)->order($sort, $order)->paginate($limit);

			$items = $query->items();
			$address = new \Net\IpLocation( app()->getRootPath() . 'data' . DIRECTORY_SEPARATOR . 'qqwry.dat');
			foreach ( $items as &$value ) {
				$value['addr'] = $address->getAddrName($value['ip']);
			}
			return json(['code'=> 0, 'msg'=> '', 'count'=> $query->total(), 'data'=> $items ]);
		}

		return json(['code'=> 403, 'msg'=> '非法提交你的ip已被记录!']);
	}

	/**
	 * 登录日志
	 *
	 * @return
	 */
	public function loginRecord(Request $request)
	{

		if( $request->isDelete() ) {
			if($this->hasPermission($request->sessInfo, 'login-record:delete')) {
				return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
			}
			$beginTime = microtime(true);
			Db::table('cfg_logs')->where('companyId', $request->sessInfo['companyId'])->where('type', 1)->where('time', '<=', time() - ( 86400 * 90 ))->delete();
			$endTime = microtime(true);
			$this->cfg_write_oper($request, $request->sessInfo['token'], $request->sessInfo['companyId'], $request->sessInfo['nickname'],
			$request->sessInfo['username'], round($endTime - $beginTime, 3), '登录日志', '清除三个月之前日志', 0, null);
			return json(['code'=> 0, 'msg'=> '删除成功!']);
		}

		if( $request->isGet() ) {
			if($this->hasPermission($request->sessInfo, 'login-record:read')) {
				return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
			}

			$where = [];
			$limit = $request->param('limit/d', 20);
			$sort = $request->param('sort', 'id', 'trim');
			$order = $request->param('order', 'desc', 'trim');
			if($sort == 'addr'){
				$sort = 'ip';
			}

			array_push( $where, ['companyId', '=', $request->sessInfo['companyId'] ] );
			array_push( $where, ['type', '=', 1 ] );

			$username = $request->param('username', '', 'trim');
			if($username) {
				array_push( $where, ['user', 'like',  "%{$username}%" ] );
			}

			$createTimeStart = $request->param('createTimeStart', '', 'trim');
			$createTimeEnd = $request->param('createTimeEnd', '', 'trim');
			if( $createTimeStart && $createTimeEnd ) {
				array_push( $where, ['time', 'between', cloud_datetime_format( $createTimeStart, $createTimeEnd, true) ] );
			}

			$query = Db::name('cfg_logs')->field(' `id`, `user`, `nickname`, `method`, `device`, `browser`, `cookie`, `referer`, `operType`, `url`, FROM_UNIXTIME(time, \'%Y-%m-%d %H:%i:%s\') as `time`, `ip`')
				->where($where)->order($sort, $order)->paginate($limit);

			$items = $query->items();
			$address = new \Net\IpLocation( app()->getRootPath() . 'data' . DIRECTORY_SEPARATOR . 'qqwry.dat');
			foreach ( $items as &$value ) {
				$value['addr'] = $address->getAddrName($value['ip']);
			}

			return json(['code'=> 0, 'msg'=> '', 'count'=> $query->total(), 'data'=> $items ]);
		}

		return json(['code'=> 403, 'msg'=> '非法提交你的ip已被记录!']);

	}

}
